package com.bothin.experiment4.controller;


import com.bothin.experiment4.constant.EmployeeTypeConstant;
import com.bothin.experiment4.enums.EmployeeEnum;
import com.bothin.experiment4.model.Employee;
import com.bothin.experiment4.service.IEmployeeService;
import com.bothin.experiment4.utils.KeyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by bothin
 * At 2019/12/15 12:43
 * Description:
 */
@Controller
@RequestMapping("/employee")
@Secured("ROLE_USER_ADMIN")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/salesman/list")
    public ModelAndView salesmanList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "8") Integer size,
                                     @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                     Map<String, Object> map){

        PageHelper.startPage(page,size);
        List<Employee> employees = null;
        if (!StringUtils.isEmpty(keyword)){
            employees = employeeService.findSalesmanByKeyword(keyword);
            map.put("key","&keyword="+keyword);
        }else {
            employees = employeeService.findAllSalesmans();
        }
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);

        map.put("pageInfo", pageInfo);
        return new ModelAndView("employee/salesman_list",map);
    }

    @GetMapping("/salesman/delete")
    public ModelAndView salesmanDelete(@RequestParam(value = "employeeId") Integer id,
                                       @RequestParam(value = "page") Integer page){

        Employee employee = employeeService.findById(id);
        if (employee.getUsable().equals(EmployeeTypeConstant.USABLE)){
            employee.setUsable(EmployeeTypeConstant.DISUSABLE);
        }else {
            employee.setUsable(EmployeeTypeConstant.USABLE);
        }
        employeeService.update(employee);

        return new ModelAndView("redirect:/employee/salesman/list?page="+page);
    }

    @GetMapping("/salesman/to/add")
    public ModelAndView salesmanToAdd(@RequestParam(value = "employeeId",required = false) Integer employeeId,
                                      @RequestParam(value = "page",required = false) Integer page,
                                      Map<String, Object> map){
        if (!StringUtils.isEmpty(employeeId)){
            Employee employee = employeeService.findById(employeeId);
            map.put("employee", employee);
            map.put("page", page);
        }
        return new ModelAndView("employee/salesman_add",map);
    }

    @PostMapping("/salesman/add")
    @Transactional
    public ModelAndView salesmanAdd(Employee employee,@RequestParam(value = "page",required = false) Integer page){
        if (employee.getEmployeeId()==null){
            employee.setAccount(KeyUtil.genEmployeeKey(EmployeeTypeConstant.ADD_SALE_MAN));
            employee.setRole(EmployeeEnum.SALESMAN.getName());
            employee.setUsable(EmployeeTypeConstant.USABLE);
            if (StringUtils.isEmpty(employee.getPassword()))
                employee.setPassword(employee.getPhone());
            employeeService.insert(employee);
        }else {
            employeeService.update(employee);
        }
        return new ModelAndView("redirect:/employee/salesman/list?page="+page);
    }

    @GetMapping("/stockman/list")
    public ModelAndView stockmanList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "8") Integer size,
                                     @RequestParam(value = "keyword", defaultValue = "") String keyword,
                                     Map<String, Object> map){
        PageHelper.startPage(page,size);
        List<Employee> employees = null;
        if (!StringUtils.isEmpty(keyword)){
            employees = employeeService.findStockmanByKeyword(keyword);
            map.put("key","&keyword="+keyword);
        }else {
            employees = employeeService.findAllStockmans();
        }
        PageInfo<Employee> pageInfo = new PageInfo<>(employees);

        map.put("pageInfo", pageInfo);
        return new ModelAndView("employee/stockman_list",map);
    }

    @GetMapping("/stockman/to/add")
    public ModelAndView stockmanToAdd(@RequestParam(value = "employeeId",required = false) Integer employeeId,
                                      @RequestParam(value = "page",required = false) Integer page,
                                      Map<String, Object> map){
        if (!StringUtils.isEmpty(employeeId)){
            Employee employee = employeeService.findById(employeeId);
            map.put("employee", employee);
            map.put("page", page);
        }
        return new ModelAndView("employee/stockman_add",map);
    }

    @PostMapping("/stockman/add")
    @Transactional
    public ModelAndView stockmanAdd(Employee employee,@RequestParam(value = "page",required = false) Integer page){
        if (employee.getEmployeeId()==null){
            employee.setAccount(KeyUtil.genEmployeeKey(EmployeeTypeConstant.ADD_STOCKMAN));
            employee.setRole(EmployeeEnum.STOCKMAN.getName());
            employee.setUsable(EmployeeTypeConstant.USABLE);
            if (StringUtils.isEmpty(employee.getPassword()))
                employee.setPassword(employee.getPhone());
            employeeService.insert(employee);
        }else {
            employeeService.update(employee);
        }
        return new ModelAndView("redirect:/employee/stockman/list?page="+page);
    }

    @GetMapping("/stockman/delete")
    public ModelAndView stockmanDelete(@RequestParam(value = "employeeId") Integer id,
                                       @RequestParam(value = "page") Integer page){

        Employee employee = employeeService.findById(id);
        if (employee.getUsable().equals(EmployeeTypeConstant.USABLE)){
            employee.setUsable(EmployeeTypeConstant.DISUSABLE);
        }else {
            employee.setUsable(EmployeeTypeConstant.USABLE);
        }
        employeeService.update(employee);

        return new ModelAndView("redirect:/employee/stockman/list?page="+page);
    }

}
